package com.yatuhashi.manual.mhp3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;

import com.yatuhashi.manual.mhp3.bean.QuestBean;
import com.yatuhashi.manual.mhp3.db.DataBaseHelper;

public class QuestListActivity extends BaseActivity {
    private ExpandableListView mExpandableListView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quest_list_activity);
        
        initHeader("キークエストリスト");
        
        initList();
    }
    
    private List<QuestBean> parents;
    private List<QuestBean> childs;
    protected void initList() {
        parents = getParents();
        childs = getChilds();
        
        List<Map<String, Object>> parentList = new ArrayList<Map<String,Object>>();
        List<List<Map<String, Object>>> allChildList = new ArrayList<List<Map<String,Object>>>();
        
        for(QuestBean parent : parents) {
            Map<String, Object> parentData = new HashMap<String, Object>();
            parentData.put("TITLE", parent.getType()+" "+parent.getStar());
            parentList.add(parentData);
            
            List<Map<String, Object>> childList = new ArrayList<Map<String,Object>>();
            for(QuestBean child : childs) {
                if(child.getTid()==parent.getTid()) {
                    Map<String, Object> childData = new HashMap<String, Object>();
                    childData.put("TITLE", child.getTitle());
                    childData.put("QID", Integer.toString(child.getQid()));
                    childList.add(childData);
                }
            }
            
            allChildList.add(childList);
        }
        
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this,
                parentList,
                R.layout.row_expandablelist_parent,
                new String[]{"TITLE"},
                new int[]{R.id.text1},
                allChildList,
                R.layout.row_expandablelist_child,
                new String[] {"TITLE"},
                new int[]{R.id.text1});
        
        mExpandableListView = (ExpandableListView) findViewById(R.id.list);
        mExpandableListView.setAdapter(adapter);
        
        mExpandableListView.setGroupIndicator(null);
        
        mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				ExpandableListAdapter adapter = parent.getExpandableListAdapter();
				
				@SuppressWarnings("unchecked")
				Map<String, Object> childMap = (Map<String, Object>) adapter.getChild(groupPosition, childPosition);
				goToQuestDetail(childMap);
				return false;
			}
		});
    }
    
    private void goToQuestDetail(Map<String, Object> childMap) {
    	String qid = (String) childMap.get("QID");
    	Intent intent = new Intent(this, QuestDetailActivity.class);
    	intent.putExtra("qid", qid);
    	startActivity(intent);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.top_icon:
            finish();
            break;
        case R.id.app_title:
            for(int i=0; i<parents.size(); i++) {
                mExpandableListView.collapseGroup(i);
            }
            break;
        case R.id.left_btn:
            toast("search button");
            break;
        case R.id.right_btn:
            toast("share button");
            break;
        default:
            break;
        }
    }
    
    private List<QuestBean> getParents() {
        DataBaseHelper dbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        List<QuestBean> list = new ArrayList<QuestBean>();
        
        QuestBean quest;
        String sql = "select type, star, tid from quest_table";
        
        Cursor cur = db.rawQuery(sql, null);
        if(cur.moveToFirst()) {
            do {
                quest = new QuestBean();
                quest.setType(cur.getString(cur.getColumnIndex("type")));
                quest.setStar(cur.getString(cur.getColumnIndex("star")));
                quest.setTid(cur.getInt(cur.getColumnIndex("tid")));
                if(notContain(list, quest)) {
                    list.add(quest);
                }
            } while(cur.moveToNext());
        }
        cur.close();
        db.close();
        return list;
    }
    
    private boolean notContain(List<QuestBean> list, QuestBean quest) {
        for(QuestBean bean : list) {
            if(quest.getTid()==bean.getTid()) {
                return false;
            }
        }
        return true;
    }
    
    private List<QuestBean> getChilds() {
        DataBaseHelper dbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        List<QuestBean> list = new ArrayList<QuestBean>();
        
        QuestBean quest;
        String sql = "select type, star, title, tid, qid from quest_table";
        
        Cursor cur = db.rawQuery(sql, null);
        if(cur.moveToFirst()) {
            do {
                quest = new QuestBean();
                quest.setType(cur.getString(cur.getColumnIndex("type")));
                quest.setStar(cur.getString(cur.getColumnIndex("star")));
                quest.setTitle(cur.getString(cur.getColumnIndex("title")));
                quest.setTid(cur.getInt(cur.getColumnIndex("tid")));
                quest.setQid(cur.getInt(cur.getColumnIndex("qid")));
                list.add(quest);
            } while(cur.moveToNext());
        }
        cur.close();
        db.close();
        return list;
    }
}
